<template>
  <div class="login-box">
    <van-cell-group>
    <van-field
        v-model="username"
        label="用户名"
        placeholder="请输入用户名"
    />
    <van-field
        v-model="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
    />
    </van-cell-group>
    <van-button round type="info" @click="signup(username, password)">登录</van-button>
  </div>
</template>

<script>
import axios from 'axios'
import { Toast } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
     username: '',
     password: ''
    }
  },
  methods: {
    signup (name, pwd) {
     let that = this
      axios.get('/static/mock/users.json')
        .then(function (res) {
          if (res.data.msg) {
            let data = res.data.data[0]
            if (data.name !== name || data.pwd !== pwd) {
             Toast.fail('用户名或密码错误,请重新输入')
             return
            }
            that.$router.push('/map')
          }
        })
        .catch( function (error) {
            console.log(error)
        } )
    }
   }
}
</script>

<style scoped>
.login-box {
  width: 100%;
  height: 100%;
}
.van-button {
  width: 90%;
  margin-top: 1rem
}
</style>
